//
//  BA_CustomerTextView.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/21.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BA_CustomerTextView.h"

@implementation BA_CustomerTextView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        UILabel *label=[[UILabel alloc]init];
        label.numberOfLines=0;
        label.backgroundColor=[UIColor clearColor];
        [self addSubview:label];
        self.placeholderLabel=label;
        self.placeholderLabel.font=[UIFont systemFontOfSize:13];
        self.placeholderLabel.textColor=UIColorHex(0xBBBBBB);
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
        
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.placeholderLabel.y = 10;
    self.placeholderLabel.x = 10;
    self.placeholderLabel.width = self.width - 2 * 10;
    // 根据文字计算label的高度
    CGSize maxSize = CGSizeMake(self.placeholderLabel.width, MAXFLOAT);

    CGSize placehoderSize=[self.placeholderLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} context:nil].size;
    self.placeholderLabel.height = placehoderSize.height;
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - 监听文字改变
- (void)textDidChange
{
    self.placeholderLabel.hidden = (self.text.length != 0);
}

- (void)setIsShowPlaceHold:(BOOL)isShowPlaceHold
{
    self.placeholderLabel.hidden = !isShowPlaceHold;
}

@end
